import { IStoreState } from '@/interfaces/store.interface';
import Vuex, { GetterTree } from 'vuex';
import Vue from 'vue';
import _ from 'lodash';

Vue.use(Vuex);

const state: IStoreState = {
  token: '',
  srand: '',
  accountInfo: {
    // acoid: '',
    // nickname: '',
    // profile: ''
    // 当前登录用户信息
    acoid: '240012',
    nickname: 'TFQ',
    profile:
      'https://test-cloud-edgeros.oss-cn-hangzhou.aliyuncs.com/edgeros-public-resource/42f6c026-79e1-40f7-a05b-2fdd40e1690c.jpeg'
  },
  managers: [],
  devices: [],
  rooms: [],
  curRoom: {
    id: '',
    name: '',
    create_time: '',
    creator: {
      acoid: '',
      nickname: '',
      profile: ''
    },
    devids: [],
    status: false,
    settings: {
      illu: 30,
      ac_cp: [],
      light_cp: []
    },
    count: 0
  },
  activeBar: 'room-list'
};

const getters = {
  isManager: (state: IStoreState) => {
    const { managers, accountInfo } = state;
    if (accountInfo.acoid) {
      return _.findIndex(managers, { acoid: accountInfo.acoid }) >= 0;
    }
    return false;
  }
};

export default new Vuex.Store({
  state,
  getters,
  mutations: {
    update(state, data) {
      Object.keys(data).forEach((key: string) => {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (state as any)[key] = data[key];
      });
    }
  }
});
